"use strict";

const Controller = require("egg").Controller;

class BrandController extends Controller {
  /**
   * 获取品牌列表
   */
  async index() {
    const { ctx } = this;
    const { name, page, pageSize } = ctx.query;

    const brands = await ctx.service.brand.getList({
      name,
      page: parseInt(page),
      pageSize: parseInt(pageSize),
    });

    ctx.body = {
      code: 0,
      message: "success",
      data: brands,
    };
  }

  /**
   * 获取品牌详情
   */
  async show() {
    const { ctx } = this;
    const { id } = ctx.params;

    const brand = await ctx.service.brand.getById(id);

    ctx.body = {
      code: 0,
      message: "success",
      data: brand,
    };
  }

  /**
   * 创建品牌
   */
  async create() {
    const { ctx } = this;
    const { name, logo, description, website, sort_order, is_show } =
      ctx.request.body;

    const brand = await ctx.service.brand.create({
      name,
      logo,
      description,
      website,
      sort_order,
      is_show,
    });

    ctx.body = {
      code: 0,
      message: "创建成功",
      data: brand,
    };
  }

  /**
   * 更新品牌
   */
  async update() {
    const { ctx } = this;
    const { id } = ctx.params;
    const { name, logo, description, website, sort_order, is_show } =
      ctx.request.body;

    const result = await ctx.service.brand.update(id, {
      name,
      logo,
      description,
      website,
      sort_order,
      is_show,
    });

    ctx.body = {
      code: 0,
      message: "更新成功",
      data: result,
    };
  }

  /**
   * 删除品牌
   */
  async destroy() {
    const { ctx } = this;
    const { id } = ctx.params;

    const result = await ctx.service.brand.delete(id);

    ctx.body = {
      code: 0,
      message: "删除成功",
      data: result,
    };
  }
}

module.exports = BrandController;
